package com.ruoyi.xm.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.xm.controller.WebSocketServer;
import com.ruoyi.xm.domain.Project;
import com.ruoyi.xm.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xm.mapper.TaskMapper;
import com.ruoyi.xm.domain.Task;
import com.ruoyi.xm.service.ITaskService;

/**
 * 任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-17
 */
@Service("taskServiceImpl")
public class TaskServiceImpl implements ITaskService 
{
    @Autowired
    private TaskMapper taskMapper;

    /**
     * 查询任务
     * 
     * @param id 任务主键
     * @return 任务
     */
    @Override
    public Task selectTaskById(Long id)
    {
        return taskMapper.selectTaskById(id);
    }

    /**
     * 查询任务列表
     * 
     * @param task 任务
     * @return 任务
     */
    @Override
    public List<Task> selectTaskList(Task task)
    {
        return taskMapper.selectTaskList(task);
    }

    @Autowired
    ProjectMapper projectMapper;
    /**
     * 新增任务
     * 
     * @param task 任务
     * @return 结果
     */
    @Override
    public int insertTask(Task task)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        task.setCreateBy(String.valueOf(userId));
        task.setCreateTime(DateUtils.getNowDate());

         taskMapper.insertTask(task);
        Project project=new Project();
        project.setId(task.getProjectId());
        Project project1 = projectMapper.selectProjectById(task.getProjectId());
        project1.setTaskSl(project1.getTaskSl()+1);
        project1.setToBeCompletedTask(project1.getToBeCompletedTask()+1);
        projectMapper.updateProject(project1);
        return 1;
    }

    /**
     * 修改任务
     * 
     * @param task 任务
     * @return 结果
     */
    @Override
    public int updateTask(Task task)
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        task.setUpdateBy(String.valueOf(userId));
        task.setUpdateTime(DateUtils.getNowDate());
        if(5==task.getStatus()){
            Project project=new Project();
            project.setId(task.getProjectId());
            Project project1 = projectMapper.selectProjectById(project.getId());
            project.setToBeCompletedTask(project1.getToBeCompletedTask()-1);
            projectMapper.updateProject(project);
        }else if(2==task.getStatus()){
            WebSocketServer.sendMessageToSpecificClient(task.getAssignedUserId().toString(),"任务:"+task.getTitle()+",指派给您,请查看");
        }else if(3==task.getStatus()){
            WebSocketServer.sendMessageToSpecificClient(task.getCreateBy(),"任务:"+task.getTitle()+",已查收");
        }
        else if(4==task.getStatus()){
            WebSocketServer.sendMessageToSpecificClient(task.getCreateBy(),"任务:"+task.getTitle()+",已完成,请查验");
        }
        return taskMapper.updateTask(task);
    }

    /**
     * 批量删除任务
     * 
     * @param ids 需要删除的任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskByIds(Long[] ids)
    {
        return taskMapper.deleteTaskByIds(ids);
    }

    /**
     * 删除任务信息
     * 
     * @param id 任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskById(Long id)
    {
        return taskMapper.deleteTaskById(id);
    }
    public void message(){
        List<Task> list = taskMapper.selectToBeContinuedTaskList();
        for (Task task1 : list) {
            WebSocketServer.sendMessageToSpecificClient(task1.getAssignedUserId().toString(),"您有任务未完成,请及时完成");
        }
    }
}
